package petstore.web.servlet.order;

import petstore.domain.CartItem;
import petstore.domain.Item;
import petstore.persistence.CartDaoImpl;
import petstore.persistence.ItemDaoImpl;
import petstore.persistence.impl.ItemDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/order")
public class OrderServlet extends HttpServlet
{
    private static final String ORDER_PAGE = "WEB-INF/jsp/orderPage/orderPage.jsp";
    List<CartItem> orderItemList = new ArrayList<CartItem>();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        String from = req.getParameter("from");
        HttpSession session = req.getSession();
        if(session.getAttribute("uid") == null)
        {
            System.out.println("add null");
            resp.setCharacterEncoding("UTF-8");
            resp.setHeader("content-type","text/html,charset=UTF-8");
            resp.setHeader("refresh","0.1;URL=login");
            resp.getWriter().write("<script language='javascript'>alert('\\u8bf7\\u5148\\u767b\\u5f55')</script>");
            return;
        }
        orderItemList.clear();
        BigDecimal totalPrice = new BigDecimal(0);
        if(from == null)
        {
            CartDaoImpl cartDao = new CartDaoImpl((int)session.getAttribute("uid"));
            List<CartItem> cartItemList = cartDao.getCartItemList();
            session.setAttribute("cartItemList",cartItemList);
            for (CartItem cartItem : cartItemList)
            {
                if(req.getParameter(cartItem.getItem().getItemId()) == null)
                    continue;
                orderItemList.add(cartItem);
                totalPrice = totalPrice.add(cartItem.getPrice().multiply(new BigDecimal(cartItem.getQuantity())));
            }

        }
        else if(from.equals("item"))
        {
            String itemId = req.getParameter("itemId");
            CartItem cartItem = new CartItem();
            ItemDaoImpl itemDao = new ItemDaoImpl();
            Item item = itemDao.getItem(itemId);
            cartItem.setItem(item);
            cartItem.setQuantity(1);
            cartItem.setPrice(item.getUnitPrice());
            cartItem.setUid((int)session.getAttribute("uid"));
            orderItemList.add(cartItem);
            totalPrice = cartItem.getPrice();
        }
        session.setAttribute("orderTotalPrice",totalPrice);
        session.setAttribute("orderItemList",orderItemList);
        req.getRequestDispatcher(ORDER_PAGE).forward(req,resp);
    }
}
